<?php

/*
 * Created on 08.01.2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 *
 * <example:bodyProcess></example:bodyProcess>
 */

m2import('phpinchains/psp/MethodTag.php');

class cms_geturibynameHandler extends phpinchains_psp_MethodTag {

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_EMPTY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName = 'cms:geturibyname';
    var $requiredAttributes = array(
        'name' => '[\w_\d-]+'
    );
    var $attributes = array(
        'var'
    );
    private $dba;

    function doStartTag() {
        $ctx = base_ApplicationContext::getInstance();
        $env = $ctx->getEnvWrapper();
        $this->dba = $ctx->getAccessFactory()->getInstanceOf(base_AccessFactory::$CMS);
        $dctx = $ctx->getDocumentContext();
        $buildMode = $dctx->getBuildMode();
        $siteId = $dctx->getSiteId();
        if (($buildMode == base_templateEngine_BuildModes::$EXPORT)
                || ($buildMode == base_templateEngine_BuildModes::$PRINT)
                || ($buildMode == base_templateEngine_BuildModes::$CACHE))
            $doc = $this->dba->getPubDocIdByName($this->getAttribute('name'), $siteId);
        else
            $doc = $this->dba->getDocIdByName($this->getAttribute('name'), $siteId);
        if ((int) $doc) {
            $this->related_docs[] = ((int)$doc);
            $this->registerRelatedDocuments($dctx);
            $link = base_templateEngine_TemplateEngineLinkBuilder::buildPageLink($dctx->getRoot()->getBuildMode(), $doc);
            if ($this->getAttribute('var'))
                $this->actionform->set($this->getAttribute('var'), $link);
            else
                $this->out->write($link);
        } else {
            if ($this->getAttribute('var'))
                $this->actionform->set($this->getAttribute('var'), '#not-found');
            else
                $this->out->write('#not-found');
        }
        return PSP_SKIP_BODY;
    }

    function doEndTag() {
        return PSP_SKIP_BODY;
    }

}

?>